package com.project.drugs;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;

import com.jdbc.util.JDBC_Util;
import com.mysql.jdbc.Connection;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;


class Menu extends JMenuBar{
	
	private static DrugsDAOImplements xr = new DrugsDAOImplements();
	
	//添加新药品
	static JTextField JTid1;
	static JTextField JTname1;
	static JTextField JTtype1;
	static JTextField JTguige1;
	static JTextField JTmoney1;
	static JTextField JTnumber1;
	
	//公共
	static JTextField JTid11;
	static JTextField JTnumber11;

	
	//修改
	static JTextField JTid2;
	static JTextField JTname2;
	static JTextField JTtype2;
	static JTextField JTguige2;
	static JTextField JTmoney2;
	static JTextField JTnumber2;
	
	
	
	
	
	
	public Menu(String a,String b,String c) {
		//添加新药品
		if(a.equals("添加新药品")) {
			final ExtendsJFrame frame = new ExtendsJFrame("医药仓库管理系统",600,400);
			frame.setLayout(new GridLayout(8,1));
			JPanel jp1 = new JPanel();
			JLabel JLtitle = new JLabel("添加新药品");
			JLtitle.setFont(new Font("宋体",Font.BOLD, 20));
			jp1.add(JLtitle);
			frame.add(jp1);
			
			JPanel jp6 = new JPanel();
			frame.add(jp6);
			
			
			JPanel jp2 = new JPanel();
			JLabel JLid = new JLabel("编号");
			final JLabel JLname = new JLabel("名称");
			JTid1 = new JTextField(13);
			JTname1 = new JTextField(13);
			jp2.add(JLid);
			jp2.add(JTid1);
			jp2.add(JLname);
			jp2.add(JTname1);
			frame.add(jp2);
			
			JPanel jp3 = new JPanel();
			final JLabel JLtype = new JLabel("类型");
			final JLabel JLguige = new JLabel("规格");
			JTtype1 = new JTextField(13);
			JTguige1 = new JTextField(13);
			jp3.add(JLtype);
			jp3.add(JTtype1);
			jp3.add(JLguige);
			jp3.add(JTguige1);
			frame.add(jp3);
			
			
			JPanel jp4 = new JPanel();
			final JLabel JLmoney = new JLabel("单价");
			final JLabel JLnumber = new JLabel("数量");
			JTmoney1 = new JTextField(13);
			JTnumber1 = new JTextField(13);
			jp4.add(JLmoney);
			jp4.add(JTmoney1);
			jp4.add(JLnumber);
			jp4.add(JTnumber1);
			frame.add(jp4);
			
			JPanel jp8 = new JPanel();
			frame.add(jp8);
			
			
			
			JButton jb1 = new JButton("确定");
			JButton jb2 = new JButton("取消");
			JPanel jp7 = new JPanel();
			jp7.add(jb1);
			jp7.add(jb2);
			frame.add(jp7);
			jb1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if(note1(Integer.parseInt(JTid1.getText()),JTname1.getText())==-1) {
						if(Double.parseDouble(JTmoney1.getText())<=0||Integer.parseInt(JTnumber1.getText())<=0) {
							JOptionPane.showMessageDialog(null,"单价或数量有误","错误 ",0);
						}
						else {
							int gc=Integer.parseInt(JTid1.getText());
							Connection conn = null;
							try {
								conn = JDBC_Util.getConnection();
								Drugs drugs = null;
								drugs = new Drugs(gc,JTname1.getText(),JTtype1.getText(),JTguige1.getText(),Double.parseDouble(JTmoney1.getText()),Integer.parseInt(JTnumber1.getText()));
								xr.insert(conn, drugs);
							} catch (Exception e1) {
								e1.printStackTrace();
							}finally {
								JDBC_Util.closeResource(conn, null);
							}
							
							JOptionPane.showMessageDialog(null,"添加成功","提示",1);
							frame.dispose();
						}
						
						
					}
					else {
						JOptionPane.showMessageDialog(null,"编号或名称已存在","错误 ",0);
					}
					
				}
			});
			jb2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
				}
			});	
		}
		else {
			final ExtendsJFrame frame2 = new ExtendsJFrame("医药仓库管理系统",600,400);
			frame2.setLayout(new GridLayout(8,1));
			JPanel JP1 = new JPanel();
			JLabel JL1 = new JLabel(a);
			JL1.setFont(new Font("宋体",Font.BOLD, 20));
			JP1.add(JL1);
			frame2.add(JP1);

			JPanel JP2 = new JPanel();
			frame2.add(JP2);

			JPanel JP5 = new JPanel();
			frame2.add(JP5);


			JPanel JP3= new JPanel();
			JLabel JLid2 = new JLabel(b);
			JTid11 = new JTextField(13);
			JP3.add(JLid2);
			JP3.add(JTid11);
			frame2.add(JP3);



			JPanel JP4 = new JPanel();
			JLabel JLname2 = new JLabel(c);
			JTnumber11 = new JTextField(13);
			JP4.add(JLname2);
			JP4.add(JTnumber11);
			frame2.add(JP4);

			JPanel JP7 = new JPanel();
			frame2.add(JP7);

			JButton JB1 = new JButton("确定");
			JButton JB2 = new JButton("取消");
			JPanel JP6 = new JPanel();
			JP6.add(JB1);
			JP6.add(JB2);
			frame2.add(JP6);
			//名称开取
			if(a.equals("按名称开取")) {
				JB1.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						int xt = note1(-1,JTid11.getText());
						if(xt==-1) {
							JOptionPane.showMessageDialog(null,"名称不存在","错误 ",0);
						}
						else {
							if(Integer.parseInt(JTnumber11.getText())<=0) {
								JOptionPane.showMessageDialog(null,"数量有误","错误 ",0);
							}
							else {
								Drugs drugs = null;
								Connection conn = null;
								try {
									conn = JDBC_Util.getConnection();
									drugs = xr.getDrugsById(conn, xt);
								} catch (Exception e1) {
									e1.printStackTrace();
								}finally {
									JDBC_Util.closeResource(conn, null);
								}
								if(Integer.parseInt(JTnumber11.getText())>drugs.getNumber()) {
									JOptionPane.showMessageDialog(null,"库存不足","错误 ",0);
								}
								else {
									drugs.setNumber_(Integer.parseInt(JTnumber11.getText()));
									JOptionPane.showMessageDialog(null,"开取成功,共需支付金额"+drugs.setMoney(Integer.parseInt(JTnumber11.getText()))+"元","提示",1);
									Connection conn1 = null;
									try {
										conn1 = JDBC_Util.getConnection();
										xr.update(conn1, drugs, xt);
									} catch (Exception e1) {
										e1.printStackTrace();
									}finally {
										JDBC_Util.closeResource(conn1, null);
									}
								}
								frame2.dispose();
							}
						}
						
					}
				});
				JB2.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						frame2.dispose();
					}
				});
			}
			//管理药品
			else if(a.equals("修改药品")||a.equals("删除药品")){
				JB1.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						int xt = note2(Integer.parseInt(JTid11.getText()),JTnumber11.getText());
						if(xt==-1) {
							JOptionPane.showMessageDialog(null,"编号或名称不存在","错误 ",0);
						}
						else {
							if(a.equals("修改药品")) {
								note4();
							}
							else {
								int a = JOptionPane.showConfirmDialog(null,"确定要删除吗？","删除提示",0);
								if(a==0) {
									note3(xt);
									JOptionPane.showMessageDialog(null,"删除成功","提示",1);
								}
							}
							frame2.dispose();
						}
						
					}
				});
				JB2.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						frame2.dispose();
					}
				});
			}
			//编号添加 开取
			else {
				JB1.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						int xt = note1(Integer.parseInt(JTid11.getText()),null);
						if(xt==-1) {
							JOptionPane.showMessageDialog(null,"编号不存在","错误 ",0);
						}
						else {
							if(Integer.parseInt(JTnumber11.getText())<=0) {
								JOptionPane.showMessageDialog(null,"数量有误","错误 ",0);
							}
							else {
								Drugs drugs = null;
								Connection conn = null;
								try {
									conn = JDBC_Util.getConnection();
									drugs = xr.getDrugsById(conn, xt);
									if(a.equals("添加原有药品")) {
										drugs.set_Number(Integer.parseInt(JTnumber11.getText()));
										xr.update(conn, drugs, drugs.getId());
										
										JOptionPane.showMessageDialog(null,"添加成功","提示",1);
									}
									else {
										if(Integer.parseInt(JTnumber11.getText())>drugs.getNumber()) {
											JOptionPane.showMessageDialog(null,"库存不足","错误 ",0);
										}
										else {
											drugs.setNumber_(Integer.parseInt(JTnumber11.getText()));
											xr.update(conn, drugs, drugs.getId());
											JOptionPane.showMessageDialog(null,"开取成功,共需支付金额"+drugs.setMoney(Integer.parseInt(JTnumber11.getText()))+"元","提示",1);
										}
									}
								} catch (Exception e1) {
									e1.printStackTrace();
								}finally {
									JDBC_Util.closeResource(conn, null);
								}
								
								frame2.dispose();
							}
						}
						
					}
				});
				JB2.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						frame2.dispose();
					}
				});
			}
		}	
	}
	//查看库存
	public Menu() {
		final ExtendsJFrame jf = new ExtendsJFrame("医药仓库管理系统",600,400);
		JTabbedPane tabbedPane=new JTabbedPane();
		Connection conn = null;
		ArrayList<Drugs> list = null;
		try {
			conn = JDBC_Util.getConnection();
			list = (ArrayList<Drugs>) xr.getAll(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			JDBC_Util.closeResource(conn, null);
		}
		int x = list.size();
		
		Drugs[] drugs = new Drugs[x];
		list.toArray(drugs);
		
		
        tabbedPane.addTab("编号升序",null,maketable(x,drugs,0),"查看库存");
        tabbedPane.addTab("编号降序",null,maketable(x,drugs,1),"查看库存");
        
        
        tabbedPane.addTab("单价升序",null,maketable(x,drugs,2),"查看库存");
        tabbedPane.addTab("单价降序",null,maketable(x,drugs,3),"查看库存");

        tabbedPane.addTab("数量升序",null,maketable(x,drugs,4),"查看库存");
        tabbedPane.addTab("数量降序",null,maketable(x,drugs,5),"查看库存");
        
        jf.add(tabbedPane,BorderLayout.CENTER);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
	}
	//验证1
	static int note1(int id,String name) {
		Connection conn = null;
		java.util.List<Drugs> list = null;
		try {
			conn = JDBC_Util.getConnection();
			list = xr.getAll(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			JDBC_Util.closeResource(conn, null);
		}
		for(int i=0;i<list.size();i++) {
			if(id==list.get(i).getId()||list.get(i).getName().equals(name)) {
				return list.get(i).getId();
			}
		}
		return -1;
	}
	//验证2
	static int note2(int id,String name) {
		Connection conn = null;
		java.util.List<Drugs> list = null;
		try {
			conn = JDBC_Util.getConnection();
			list = xr.getAll(conn);
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			JDBC_Util.closeResource(conn, null);
		}
		for(int i=0;i<list.size();i++) {
			if(id==list.get(i).getId()&&list.get(i).getName().equals(name)) {
				return id;
			}
		}
		return -1;
	}
	//删除
	static void note3(int id) {
		Connection conn = null;
		try {
			conn = JDBC_Util.getConnection();
			xr.deleteById(conn, id);
		} catch (Exception e1) {
			e1.printStackTrace();
		}finally {
			JDBC_Util.closeResource(conn, null);
		}
	}
	//修改
	static void note4() {
		final ExtendsJFrame frame = new ExtendsJFrame("医药仓库管理系统",600,400);
		frame.setLayout(new GridLayout(8,1));
		JPanel jp1 = new JPanel();
		JLabel JLtitle = new JLabel("修改药品");
		JLtitle.setFont(new Font("宋体",Font.BOLD, 20));
		jp1.add(JLtitle);
		frame.add(jp1);
		
		JPanel jp6 = new JPanel();
		frame.add(jp6);
		
		
		JPanel jp2 = new JPanel();
		JLabel JLid = new JLabel("编号");
		JTid2  = new JTextField(13);
		final JLabel JLname = new JLabel("名称");
		JTname2 = new JTextField(13);
		jp2.add(JLid);
		jp2.add(JTid2);
		jp2.add(JLname);
		jp2.add(JTname2);
		frame.add(jp2);
		
		JPanel jp3 = new JPanel();
		final JLabel JLtype = new JLabel("类型");
		JTtype2 = new JTextField(13);
		final JLabel JLguige = new JLabel("规格");
		JTguige2 = new JTextField(13);
		jp3.add(JLtype);
		jp3.add(JTtype2);
		jp3.add(JLguige);
		jp3.add(JTguige2);
		frame.add(jp3);
		
		
		JPanel jp4 = new JPanel();
		final JLabel JLmoney = new JLabel("单价");
		JTmoney2 = new JTextField(13);
		final JLabel JLnumber = new JLabel("数量");
		JTnumber2 = new JTextField(13);
		jp4.add(JLmoney);
		jp4.add(JTmoney2);
		jp4.add(JLnumber);
		jp4.add(JTnumber2);
		frame.add(jp4);
		
		JPanel jp8 = new JPanel();
		frame.add(jp8);
		
		
		
		JButton jb1 = new JButton("确定");
		JButton jb2 = new JButton("取消");
		JPanel jp7 = new JPanel();
		jp7.add(jb1);
		jp7.add(jb2);
		frame.add(jp7);
		jb1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int xt = note2(Integer.parseInt(JTid11.getText()),JTnumber11.getText());
				Drugs drugs = null;
				Connection conn = null;
				
				try {
					conn = JDBC_Util.getConnection();
					drugs = xr.getDrugsById(conn, xt);
				} catch (Exception e1) {
					e1.printStackTrace();
				}finally {
					JDBC_Util.closeResource(conn, null);
				}
				
				if(note1(Integer.parseInt(JTid2.getText()),JTname2.getText())==-1||Integer.parseInt(JTid2.getText())==drugs.getId()||drugs.getName().equals(JTname2.getText())) {
					if(Double.parseDouble(JTmoney2.getText())<=0||Integer.parseInt(JTnumber2.getText())<=0) {
						JOptionPane.showMessageDialog(null,"单价或数量有误","错误 ",0);
					}
					else {
						int a = JOptionPane.showConfirmDialog(null,"确定要修改吗？","修改提示",0);
						
						if(a==0) {
							
							drugs.setName(JTname2.getText());
							drugs.setType(JTtype2.getText());
							drugs.setGuige(JTguige2.getText());
							drugs.setNumber(Integer.parseInt(JTnumber2.getText()));
							drugs.setMoney(Double.parseDouble(JTmoney2.getText()));
							if(Integer.parseInt(JTid2.getText())>=0) {
								drugs.setId(Integer.parseInt(JTid2.getText()));
							}
							Connection conn1 = null;
							try {
								conn1 = JDBC_Util.getConnection();
								xr.update(conn1, drugs, xt);
							} catch (Exception e1) {
								e1.printStackTrace();
							}finally {
								JDBC_Util.closeResource(conn1, null);
							}
							JOptionPane.showMessageDialog(null,"修改成功","提示",1);
						}
						frame.dispose();
					}
				}
				else {
					JOptionPane.showMessageDialog(null,"编号或名称已存在","错误 ",0);
				}
			}
		});
		jb2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
	}
	//表格居中
	static void setTableColumnCenter(JTable table){
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();   
		r.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class, r);
	}
	//创建表格
	static JScrollPane maketable(int x,Drugs[] drugs,int smz) {
		Object[][] tableDate=new Object[x][6];
        String[] name = {"编号","名称","类型","规格","单价","数量"};
        makeDrugs(tableDate,x,drugs,smz);
        JTable table = new JTable(tableDate,name);
        setTableColumnCenter(table);
        JScrollPane pane = new JScrollPane(table);
        return pane;
	}
	//输入表格
	static void makeDrugs(Object[][] tableDate,int x,Drugs[] drugs,int smz) {
		switch(smz){
			case 0:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return Integer.compare(o1.getId(), o2.getId());
					}
				});
				break;
				
				
			case 1:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return -Integer.compare(o1.getId(), o2.getId());
					}
				});
				break;
				
			case 2:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return Double.compare(o1.getMoney(), o2.getMoney());
					}
				});
				break;
			case 3:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return -Double.compare(o1.getMoney(), o2.getMoney());
					}
				});
				break;
			case 4:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return Double.compare(o1.getNumber(), o2.getNumber());
					}
				});
				break;
			case 5:
				Arrays.sort(drugs, new Comparator<Drugs>() {
					@Override
					public int compare(Drugs o1, Drugs o2) {
						return -Double.compare(o1.getNumber(), o2.getNumber());
					}
				});
				break;
		}
		for(int i=0;i<x;i++) {
			tableDate[i][0]=drugs[i].getId();
			tableDate[i][1]=drugs[i].getName();
			tableDate[i][2]=drugs[i].getType();
			tableDate[i][3]=drugs[i].getGuige();
			tableDate[i][4]=drugs[i].getMoney();
			tableDate[i][5]=drugs[i].getNumber();
		}
    }
	public static void main(String[] args) {
		new Login();
	}
}



